﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace TreasureBox.Utility
{
    public class StockProcess
    {
        static string file = "Stock.xml";
        public static string AddStock(string stockCode)
        {
            XmlDocument document = new XmlDocument();
            document.Load(file);
            if (IsExistence(stockCode) == false)
            {
                XmlElement element = document.CreateElement("Stock");
                XmlText stock = document.CreateTextNode("stockCode");
                element.AppendChild(stock);
                document.Save(file);
                return "The stock has been successfully added!";
            }
            else
            {
                return "The stock has already been existed in the stock list!";
            }
        }
        public static bool IsExistence(string stockCode)
        {
            XmlDocument document = new XmlDocument();
            document.Load(file);
            foreach (XmlNode node in document.GetElementsByTagName("Stock"))
            {
                if (node.ChildNodes[0].NodeType == XmlNodeType.Text)
                    if (node.ChildNodes[0].Value == stockCode)
                    {
                        return true;
                    }
                    else
                    {
                        continue;
                    }
            }
            return false;
        }
        public static ArrayList GetStockList()
        {
            ArrayList StockList = new ArrayList();

            XmlDocument document = new XmlDocument();
            document.Load(file);
            foreach (XmlNode node in document.GetElementsByTagName("Stock"))
            {

                if (node.ChildNodes[0].NodeType == XmlNodeType.Text)
                {
                    StockList.Add(node.ChildNodes[0].Value);
                }
            }
            return StockList;
        }
    }
}
